package com.toman.ui.work.workshop;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Enum.BaseEnum;
import com.toman.Enum.PermEnum;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.JavaBean.InspectionReportDto;
import com.toman.R;
import com.toman.Util.DefaultAcitonCallback;
import com.toman.Util.DialogUtils;
import com.toman.Util.JsonUtils;
import com.toman.Util.PermissonUtils;
import com.toman.Util.SPUtils;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.bean.ItemListBean;
import com.toman.toolbar.ToolBarActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Request;
import okhttp3.Response;

public class InspectionReporInfActivity extends ToolBarActivity {
    @BindView(R.id.tv_beltline)
    TextView tvBeltline;
    @BindView(R.id.tv_product_name)
    TextView tvProductName;
    @BindView(R.id.tv_product_mode)
    TextView tvProductMode;
    @BindView(R.id.tv_qualified)
    TextView tvQualified;
    @BindView(R.id.tv_create_man)
    TextView tvCreateMan;
    @BindView(R.id.tv_create_time)
    TextView tvCreateTime;
    @BindView(R.id.ll_create)
    LinearLayout llCreate;
    @BindView(R.id.tv_beltline_title)
    TextView tvBeltlineTitle;
    @BindView(R.id.tv_product_name_title)
    TextView tvProductNameTitle;
    @BindView(R.id.tv_qualified_title)
    TextView tvQualifiedTitle;
    private InspectionReportDto bean;
    String strs[] = new String[]{"可生产", "待调整", "强制停机"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inspection_repor_inf);
        ButterKnife.bind(this);
        initToolBar(BaseEnum.INSPECTIONREPORT.name + "详情");
        initIntent();
        initView();
    }

    private void initView() {
        ViewUtil.unclick(tvBeltline, tvProductName, tvProductMode, tvQualified, tvCreateMan, tvCreateTime);
        Object[] str = new Object[]{bean.getBeltlineName(), bean.getProductName(), bean.getProductMode(), strs[bean.getStatus()], bean.getCreateUser().getRealName(), bean.getCreateTime()};
        ViewUtil.setText(str, tvBeltline, tvProductName, tvProductMode, tvQualified, tvCreateMan, tvCreateTime);


    }

    private void initIntent() {
        status = getIntent().getStringExtra("status");
        bean = (InspectionReportDto) getIntent().getSerializableExtra("bean");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toolbar_edit, menu);


        MenuItem menuItem = menu.findItem(R.id.menu_edit);
        if (!PermissonUtils.contains(getApplicationContext(), BaseEnum.INSPECTIONREPORT.has(PermEnum.EDIT))) {
            menuItem.setVisible(false);
        }
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_edit:
                        if (item.getTitle().equals("编辑")) {
                            item.setTitle("完成");
                            ViewUtil.click(tvBeltline, tvProductName, tvQualified);
                            ViewUtil.necessary(tvQualifiedTitle,tvProductNameTitle,tvProductNameTitle,tvBeltlineTitle);

                        } else {
                            add();
                        }
                        break;
                }
                return false;
            }
        });

        return true;
    }

    @OnClick({R.id.tv_beltline, R.id.tv_product_name, R.id.tv_product_mode, R.id.tv_qualified})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_beltline:
                DialogUtils.showDialog(this, "产线", NewBaseUrl.beltline_list_get, "beltlineName", new DefaultAcitonCallback() {
                    @Override
                    public void run(ItemListBean bean1, Dialog dialog) {
                        bean.setBeltlineId(Long.parseLong(bean1.getID()));
                        tvBeltline.setText(bean1.getName());
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_product_name:
                DialogUtils.showProductDialog(this, "品名", NewBaseUrl.workshop_product_list, "productName", new DefaultAcitonCallback() {
                    @Override
                    public void run(ItemListBean bean1, Dialog dialog) {
                        bean.setProductId(Long.parseLong(bean1.getID()));
                        tvProductName.setText(bean1.getName());
                        tvProductMode.setText(bean1.getRemark());
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_product_mode:

                break;
            case R.id.tv_qualified:
                DialogUtils.showDialog(this, "质量状态", strs, "", new DefaultAcitonCallback() {
                    @Override
                    public void run(ItemListBean bean1, Dialog dialog) {
                        bean.setStatus(Integer.parseInt(bean1.getID()));
                        tvQualified.setText(bean1.getName());
                        dialog.dismiss();
                    }
                });
                break;
        }
    }

    public void add() {
        String[] item = new String[]{"产线", "品名", "质量状态"};//直接初始化
        if (ViewUtil.isNull(getApplicationContext(), item, tvBeltline, tvProductName, tvQualified)) {
            return;
        }


        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beltlineId", bean.getBeltlineId());
        map.put("productId", bean.getProductId());
        map.put("status", bean.getStatus());
        map.put("id", bean.getId());


        String str = JsonUtils.obj2JsonString(map);
        OkHttpUtils
                .post(NewBaseUrl.workshop_inspectionreport_update)
                .headers("token", token)
                .headers("Content-Type", "application/json")
                .postJson(str)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", getClass().toString() + result);
                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");
                        ViewUtil.lagout(code, getApplicationContext());

                        if (code == 0) {
                            ToastUtil.makeText(getApplicationContext(), jsonObj.getString("msg"));
                            finish();

                        } else {
                            ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                        }

                    }
                });

    }
}
